#include <iostream>
#include <vector>

using namespace std;

/// 198. House Robber		自底向上(动态规划)
/// https://leetcode.com/problems/house-robber/description/
/// 记忆化搜索
/// 时间复杂度: O(n^2)
/// 空间复杂度: O(n)
class Solution {


public:
    int rob(vector<int>& nums) {
		int n = nums.size();
		if(n == 0) return 0;

		// memo[i] 表示考虑rob nums[i...n) 所能获得的最大收益
		vector<int> memo(n,0);
		memo[n-1] = nums[n-1];

		for(int i = n - 2; i >= 0; i--)
			for(int j = i; j < n; j++)
				memo[i] = max(memo[i],
						nums[j] + (j + 2 < n ? memo[j + 2] : 0));
		return memo[0];
    }
};

int main() {

    int nums[] = {2, 1, 3, 4,6,9,2,3,5,6,8,2,1,7,3,9,8,2,1,5};
    vector<int> vec(nums, nums + sizeof(nums)/sizeof(int));

    cout << Solution().rob(vec) << endl;

    return 0;
}
